package com.gao.config;

import com.gao.filters.MyFilter;
import com.gao.filters.OriginalFilter;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

/**
 * 代替Web.xml
 *
 * 可以配置过滤器
 * 指定Spring和SpringMVC的配置类位置
 * 指定dispatcher的url映射
 *
 * @author GaoXi
 * @date 2021/8/17 20:42
 */
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {
    /**
     * Spring配置类的位置
     * @return
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    /**
     * SpringMVC配置类的位置
     * @return
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{WebConfig.class};
    }

    /**
     * Dispatcher的url映射
     * @return
     */
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    /**
     * 注册过滤器
     * @return
     */
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceResponseEncoding(true);
        HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
        MyFilter myFilter = new MyFilter();
        OriginalFilter originalFilter = new OriginalFilter();
        return new Filter[]{characterEncodingFilter, hiddenHttpMethodFilter,myFilter,originalFilter};
    }
}
